home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_psutils.idb / usr / freeware / bin / psmerge.z / psmerge
Encoding:
Text File  |  1999-07-16  |  2.0 KB  |  89 lines

  1. #!/usr/freeware/bin/perl
  2. eval 'exec perl -S $0 "$@"'
  3.     if $running_under_some_shell;
  4.  
  5. # psmerge: merge PostScript files produced by same application and setup
  6. # usage: psmerge [-oout.ps] [-thorough] file1.ps file2.ps ...
  7. #
  8. # Copyright (C) Angus J. C. Duggan 1991-1995
  9. # See file LICENSE for details.
  10.  
  11. $prog = ($0 =~ s=.*/==);
  12.  
  13. while ($ARGV[0] =~ /^-/) {
  14.    $_ = shift;
  15.    if (/^-o(.+)/) {
  16.       if (!close(STDOUT) || !open(STDOUT, ">$1")) {
  17.      print STDERR "$prog: can't open $1 for output\n";
  18.      exit 1;
  19.       }
  20.    } elsif (/^-t(horough)?$/) {
  21.       $thorough = 1;
  22.    } else {
  23.       print STDERR "Usage: $prog [-oout] [-thorough] file...\n";
  24.       exit 1;
  25.    }
  26. }
  27.  
  28. $page = 0;
  29. $first = 1;
  30. $nesting = 0;
  31.  
  32. @header = ();
  33. $header = 1;
  34.  
  35. @trailer = ();
  36. $trailer = 0;
  37.  
  38. @pages = ();
  39. @body = ();
  40.  
  41. @resources = ();
  42. $inresource = 0;
  43.  
  44. while (<>) {
  45.    if (/^%%BeginFont:/ || /^%%BeginResource:/ || /^%%BeginProcSet:/) {
  46.       $inresource = 1;
  47.       push(@resources, $_);
  48.    } elsif ($inresource) {
  49.       push(@resources, $_);
  50.       $inresource = 0 if /^%%EndFont/ || /^%%EndResource/ || /^%%EndProcSet/;
  51.    } elsif (/^%%Page:/ && $nesting == 0) {
  52.       $header = $trailer = 0;
  53.       push(@pages, join("", @body)) if @body;
  54.       $page++;
  55.       @body = ("%%Page: ($page) $page\n");
  56.    } elsif (/^%%Trailer/ && $nesting == 0) {
  57.       push(@trailer, $_);
  58.       push(@pages, join("", @body)) if @body;
  59.       @body = ();
  60.       $trailer = 1;
  61.       $header = 0;
  62.    } elsif ($header) {
  63.       push(@trailer, $_);
  64.       push(@pages, join("", @body)) if @body;
  65.       @body = ();
  66.       $trailer = 1;
  67.       $header = 0;
  68.    } elsif ($trailer) {
  69.       if (/^%!/ || /%%EOF/) {
  70.      $trailer = $first = 0;
  71.       } elsif ($first) {
  72.      push(@trailer, $_);
  73.       }
  74.    } elsif (/^%%BeginDocument/ || /^%%BeginBinary/ || /^%%BeginFile/) {
  75.       push(@body, $_);
  76.       $nesting++;
  77.    } elsif (/^%%EndDocument/ || /^%%EndBinary/ || /^%%EndFile/) {
  78.       push(@body, $_);
  79.       $nesting--;
  80.    } else {
  81.       print $_ if $print;
  82.    }
  83. }
  84.  
  85. print @trailer;
  86.  
  87. exit 0;
  88.  
  89.